package com.sharkpro.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.sharkpro.entity.ElecExceptionRecords;
import com.sharkpro.service.ElecExceptionRecordsService;
import com.sharkpro.service.ImageService;
import com.sharkpro.service.SysUserService;
import com.sharkpro.utils.ResultUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author jianxin.wei
 * @description
 * @date 2024/12/16 10:12
 */
@RequestMapping("user")
@RestController
public class SysUserController {

    @Resource
    SysUserService userService;


    @Resource
    ImageService imageService;
    @RequestMapping("getIp")
    public String getIp(HttpServletRequest request) {
//        return userService.getIp();
        if (StrUtil.isBlank(request.getHeader("X-Forwarded-For"))){
            return request.getRemoteAddr();
        }
        return request.getHeader("X-Forwarded-For");
    }

    @RequestMapping("getImage")
    public ResultUtil getImage() {
        return imageService.getImage();
    }

    @RequestMapping("verifyImg")
    public ResultUtil verifyImg(@RequestBody JSONObject jsonObject) {
        String imgCode = jsonObject.getStr("imgCode");
        if (StrUtil.isBlank(imgCode)){
            return new ResultUtil().fail("请输入图片验证码");
        }
        String uuid = jsonObject.getStr("uuid");
        return imageService.verifyImg(uuid,imgCode);
    }

//    /**
//     * 登录,使用security之后就不需要自己写登录接口了
//     */
//    @RequestMapping("login")
//    public ResultUtil login(@RequestBody JSONObject jsonObject) {
//        String account = jsonObject.getStr("username");
//        String password = jsonObject.getStr("password");
//        return userService.login(account, password);
//    }

    @RequestMapping("logout")
    public ResultUtil logout(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        return userService.logout(request);
    }

    /**
     * 用户列表
     */
    @RequestMapping("list")
    public ResultUtil list(@RequestBody JSONObject jsonObject) {
        String admin = jsonObject.getStr("admin");
        return userService.list(admin);
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    public void export(HttpServletResponse response) {
        userService.export(response);
    }



}
